import {http} from 'http.js'
import {baseUrl} from 'http.js'

// 获取实名认证信息 【首次创建简历的页面】
function getSeekerInfo(){
  return http({
    url:'/api/seeker/info',
  })
}

//  保存简历
function saveSeekerInfo(data){
  return http({
    url:'/api/seeker/complete_info',
    data:data,
    method: 'POST'
  })
}

// 获取简历完善度
function getPerfectNumber(){
  return http({
    url:'/api/seeker/perfect_number',
  })
}
// 所以省集合
function getAddressLists() {
  return http({
    url: '/api/public/native_place_list'
  })
}

// 获取所有民族
function getNationList() {
  return http({
    url: '/api/public/ethnicity_list'
  })
}

// 上传图片
function uploadImageSts() {
  return http({
    url: '/api/basic/sts'
  })
}

// 获取银行卡信息
function getBankCardInfo() {
  return http({
    url: '/api/bank_card/query_bank_card'
  })
}

function getUserIdCard(){
  return http({
    url: '/api/seeker_certification/get_certification_info'
  })
}

function submitReview(data){
  return http({
    url: '/api/seeker_certification/submit_review',
    method: 'POST',
    data: data,
  })
}

function getContractList(){
  return http({
    url: '/api/seeker_contract/list_contract',
  })
}

function toContractDetail(data){
  return http({
    url: '/api/seeker_contract/get_contract_preview_url',
    data:data,
  })
}
// 查询合同状态
function checkSignStatus(data){
  return http({
    url: '/api/public/get_signer_status',
    data: data,
  })
}

function singContract(item,completed){

  let returnUrl = ''
  if( http.baseUrl === 'https://recruit-dev.lgong.com/' ){
    returnUrl = `https://static-test.lgong.com/dsqbMpSignLoading.html`
  }else if( http.baseUrl === 'https://recruit-test.lgong.com/' ){
    returnUrl = `https://static-test.lgong.com/dsqbMpSignLoading.html`
  }else if( http.baseUrl === 'https://recruit.lgong.com/' ){
    returnUrl = `https://static.lgong.com/dsqbMpSignLoading.html`
  }
  let getUrl = item.contractNo?`/api/seeker_contract/get_sign_contract_url_by_no?contractNo=${item.contractNo}&returnUrl=${returnUrl}&templateId=${item.id}`:`/api/seeker_contract/get_sign_contract_url?returnUrl=${returnUrl}&templateId=${item.id}`
  let  params = {
    url:getUrl,
  }
  requestData(params,completed);
}



// 可传出参数的请求
function requestData(params,complated) {
  wx.request({
    url: baseUrl + params.url, //服务器url+参数中携带的接口具体地址
    data: params.data||{}, //请求参数
    header: params.header || {
      'Authorization': "Bearer " + getApp().globalData.token,
      "Content-Type": "application/json" //设置后端需要的常用的格式就好，特殊情况调用的时候单独设置
    },
    method: params.method || 'GET', //默认为GET,可以不写，如常用请求格式为POST，可以设置POST为默认请求方式
    dataType: params.dataType, //返回的数据格式,默认为JSON，特殊格式可以在调用的时候传入参数
    responseType: params.responseType, //响应的数据类型
    success: function (res) {
      //接口访问正常返回数据
      if (res.statusCode == 200) {
        //1. 操作成功返回数据,原则上只针对服务器端返回成功的状态（如本例中为000000）
        if (res.data.code == "10000") {
          complated(res.data.data)
        } else if (res.data.code == 40003 || res.data.code == 40004) {
          wx.reLaunch({
            url: '/pages/login/login',
          })
        } else if (res.data.code == 20050) {
          complated(res.data.code)
        } else {
          wx.showToast({
            icon: "none",
            title: res.data.message
          })
        }
      } else {
        //2. 操作不成功返回数据，以toast方式弹出响应信息，如后端未格式化非操作成功异常信息，则可以统一定义异常提示
        wx.showToast({
          icon: "none",
          title: res.data.message
        })
      }
    },
    fail: function (e) {
      complated(e)
    }
  })
}

// 上传图片
function uploadImage(params,complated){

  wx.uploadFile({
    filePath: params.filePath,
    name: params.name,
    url: baseUrl + 'api/seeker_certification/ocr_id_card',
    header: params.header || {
      'Authorization': "Bearer " + getApp().globalData.token,
      "Content-Type": "application/json" //设置后端需要的常用的格式就好，特殊情况调用的时候单独设置
    },
    success: function(res){
      wx.hideLoading()
      complated(res)
    },
    fail: function(res){
      wx.showToast({
        icon: 'none',
        title: '上传身份证失败',
        duration: 2000
      });
    }

  })

}


module.exports = {
  getSeekerInfo: getSeekerInfo,
  saveSeekerInfo: saveSeekerInfo,
  getPerfectNumber: getPerfectNumber,
  getAddressLists: getAddressLists,
  getNationList: getNationList,
  uploadImageSts: uploadImageSts,
  getBankCardInfo: getBankCardInfo,
  getUserIdCard: getUserIdCard,
  submitReview: submitReview,
  getContractList,getContractList,
  toContractDetail,toContractDetail,
  singContract,singContract,
  uploadImage,uploadImage,
  checkSignStatus,checkSignStatus,
}